package org.summer.lawyer.controller.wx;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.summer.common.core.domain.R;
import org.summer.common.mybatis.core.page.PageQuery;
import org.summer.common.mybatis.core.page.TableDataInfo;
import org.summer.common.satoken.utils.LoginHelper;
import org.summer.lawyer.constant.SystemConstants;
import org.summer.lawyer.domain.bo.award.AwardBo;
import org.summer.lawyer.domain.bo.lawfirm.LawFirmBo;
import org.summer.lawyer.domain.bo.lawfirmtag.LawFirmTagBo;
import org.summer.lawyer.domain.bo.lawyerinfo.LawyerInfoBo;
import org.summer.lawyer.domain.bo.legalcase.LegalCaseBo;
import org.summer.lawyer.domain.bo.spucomment.SpuCommentBo;
import org.summer.lawyer.domain.vo.award.AwardVo;
import org.summer.lawyer.domain.vo.lawfirm.LawFirmArchiveUpdateVo;
import org.summer.lawyer.domain.vo.lawfirm.LawFirmVo;
import org.summer.lawyer.domain.vo.lawfirmtag.LawFirmTagVo;
import org.summer.lawyer.domain.vo.lawyerinfo.LawyerInfoVo;
import org.summer.lawyer.domain.vo.legalcase.LegalCaseVo;
import org.summer.lawyer.domain.vo.spucomment.SpuCommentVo;
import org.summer.lawyer.domain.vo.userfollow.UserFollowVo;
import org.summer.lawyer.service.*;
import org.summer.system.domain.bo.SysDictDataBo;
import org.summer.system.domain.vo.SysDictDataVo;
import org.summer.system.service.ISysDictDataService;

import java.util.List;

/**
 *
 */
@Tag(name = "微信小程序律师律所相关接口", description = "微信小程序律所相关接口")
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/wx/lawfirm")
public class WxLawFirmController {

    private final ILawFirmService lawFirmService;
    private final ILegalCaseService legalCaseService;
    private final IAwardService awardService;
    private final ILawyerInfoService lawyerInfoService;
    private final ILawFirmTagService lawFirmTagService;
    private final ISpuCommentService spuCommentService;
    private final IUserFollowService userFollowService;
    private final ISysDictDataService sysDictDataService;


    @Operation(summary = "首页推荐律师和律所接口", description = "首页推荐律所列表")
    @SaIgnore
    @GetMapping("/listHome")
    public TableDataInfo<LawFirmVo> listHome() {
        return TableDataInfo.build(lawFirmService.selectListByCurated());
    }

    @Parameters({
            @Parameter(name = "lawFirmType", description = "律所类型（1=律师，2=律所）"),
            @Parameter(name = "lawFirmName", description = "名称"),
            @Parameter(name = "wxUserId", description = "微信用户ID（查询用户是否关注）")
    })
    @Operation(summary = "律师律所列表接口", description = "律师律所列表")
    @SaIgnore
    @GetMapping("/list")
    public TableDataInfo<LawFirmVo> list(LawFirmVo vo, PageQuery pageQuery) {
        vo.setStatus(SystemConstants.ACTIVE);
        return lawFirmService.selectWechatPageList(vo, pageQuery);
    }

    @Operation(summary = "律所排名接口", description = "律所排名接口")
    @SaIgnore
    @GetMapping("/listRank")
    public TableDataInfo<LawFirmVo> listRank(LawFirmBo bo) {
        return TableDataInfo.build(lawFirmService.selectListByRating(bo));
    }

    @Parameters({
            @Parameter(name = "lawFirmId", description = "律所ID", in = ParameterIn.PATH, required = true)
    })
    @Operation(summary = "查询律所信息", description = "获取律所信息")
    @SaIgnore
    @GetMapping("/{lawFirmId}")
    public R<LawFirmVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable("lawFirmId") Long lawFirmId) {
        LawFirmVo vo = lawFirmService.selectLawFirmVoById(lawFirmId);
        if(vo != null && LoginHelper.getUserId() != null){
            //查询关注记录
            UserFollowVo userFollowVo = userFollowService.selectUserFollowVoByUserId(LoginHelper.getUserId(),lawFirmId);
            if(userFollowVo != null && "0".equals(userFollowVo.getCancelFlag())){
                vo.setFollow("1");
            }
        }
        return R.ok(vo);
    }

    @Parameters({
            @Parameter(name = "wxUserId", description = "微信用户ID", in = ParameterIn.PATH, required = true)
    })
    @Operation(summary = "通过管理员用户ID查询律所信息", description = "通过管理员用户ID查询律所信息")
    @SaCheckLogin
    @GetMapping("/manage/{wxUserId}")
    public R<LawFirmVo> getInfoByAdminId(@NotNull(message = "主键不能为空") @PathVariable("wxUserId") Long wxUserId) {
        LawFirmVo vo = lawFirmService.selectLawFirmVoByAdminId(wxUserId);
        if(vo != null && LoginHelper.getUserId() != null){
            //查询关注记录
            UserFollowVo userFollowVo = userFollowService.selectUserFollowVoByUserId(LoginHelper.getUserId(),vo.getLawFirmId());
            if(userFollowVo != null && "0".equals(userFollowVo.getCancelFlag())){
                vo.setFollow("1");
            }
        }
        return R.ok(vo);
    }


    @Operation(summary = "查询律师律所评价标签", description = "查询律师律所评价标签")
    @SaIgnore
    @GetMapping("/listTag")
    public TableDataInfo<LawFirmTagVo> listTag(LawFirmTagBo bo) {
        return TableDataInfo.build(lawFirmTagService.selectLawFirmTagVoList(bo));
    }

    @Parameters({
            @Parameter(name = "lawFirmId", description = "律所ID", in = ParameterIn.PATH, required = true)
    })
    @Operation(summary = "律所案例列表查询", description = "律所案例列表查询")
    @SaIgnore
    @GetMapping("/listCase/{lawFirmId}")
    public TableDataInfo<LegalCaseVo> listCase(@NotNull(message = "律所ID不能为空") @PathVariable("lawFirmId") Long lawFirmId) {
        LegalCaseVo vo = new LegalCaseVo();
        vo.setLawFirmId(lawFirmId);
        return legalCaseService.selectListByScope(vo);
    }

    @Parameters({
            @Parameter(name = "lawFirmId", description = "律所ID", in = ParameterIn.PATH, required = true)
    })
    @Operation(summary = "律所荣誉列表查询", description = "律所荣誉列表查询")
    @SaIgnore
    @GetMapping("/listAward/{lawFirmId}")
    public TableDataInfo<AwardVo> listAward(@NotNull(message = "律所ID不能为空") @PathVariable("lawFirmId") Long lawFirmId) {
        AwardVo vo = new AwardVo();
        vo.setLawFirmId(lawFirmId);
        return awardService.selectListByScope(vo);
    }

    @Parameters({
            @Parameter(name = "lawFirmId", description = "律所ID", in = ParameterIn.PATH, required = true)
    })
    @Operation(summary = "查询律所添加的律师列表", description = "查询律所添加的律师列表")
    @SaIgnore
    @GetMapping("/listLawyer/{lawFirmId}")
    public TableDataInfo<LawyerInfoVo> listLawyer(@NotNull(message = "律所ID不能为空") @PathVariable("lawFirmId") Long lawFirmId) {
        LawyerInfoBo bo = new LawyerInfoBo();
        bo.setLawFirmId(lawFirmId);
        return TableDataInfo.build(lawyerInfoService.selectLawyerInfoVoList(bo));
    }

    @Parameters({
            @Parameter(name = "lawFirmId", description = "律所ID", required = true),
            @Parameter(name = "special", description = "擅长", required = true),
            @Parameter(name = "lawFirmDesc", description = "律所简介", required = true),
            @Parameter(name = "lawFirmAddress", description = "律所地址", required = true)
    })
    @Operation(summary = "新增或修改律所档案信息", description = "新增或修改律所档案信息")
    @SaCheckLogin
    @PostMapping("/saveArchive")
    public R<Void> updateLawFirmArchive(@Validated @RequestBody LawFirmArchiveUpdateVo vo) {
        lawFirmService.updateLawFirmArchive(vo);
        return R.ok();
    }

    @Parameters({
            @Parameter(name = "lawFirmId", description = "律所ID", required = true),
            @Parameter(name = "title", description = "标题", required = true),
            @Parameter(name = "caseImage", description = "封面图片", required = true),
            @Parameter(name = "caseContent", description = "案例内容", required = true)
    })
    @Operation(summary = "新增或修改律所案例信息", description = "新增或修改律所案例信息")
    @SaCheckLogin
    @PostMapping("/saveCase")
    public R<Void> saveCase(@Validated @RequestBody LegalCaseBo bo) {
        legalCaseService.saveLegalCase(bo);
        return R.ok();
    }

    @Parameters({
            @Parameter(name = "ids", description = "案例主键ID(批量删除时可以传数组)", in = ParameterIn.PATH, required = true)
    })
    @Operation(summary = "删除律所案例信息", description = "删除律所案例信息")
    @SaCheckLogin
    @DeleteMapping("/case/{ids}")
    public R<Void> removeCase(@NotEmpty(message = "主键不能为空") @PathVariable("ids") Long[] ids) {
        legalCaseService.deleteLegalCaseByIds(List.of(ids));
        return R.ok();
    }

    @Parameters({
            @Parameter(name = "id", description = "主键ID", in = ParameterIn.PATH, required = true)
    })
    @Operation(summary = "获取案例详情", description = "获取案例详情")
    @SaIgnore
    @GetMapping("/case/{id}")
    public R<LegalCaseVo> getLegalCase(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return R.ok(legalCaseService.selectLegalCaseVoById(id));
    }


    @Parameters({
            @Parameter(name = "lawFirmId", description = "律所ID", required = true),
            @Parameter(name = "title", description = "标题", required = true),
            @Parameter(name = "awardImage", description = "封面图片", required = true),
            @Parameter(name = "awardContent", description = "案例内容", required = true)
    })
    @Operation(summary = "新增或修改律所荣誉信息", description = "新增或修改律所荣誉信息")
    @SaCheckLogin
    @PostMapping("/saveAward")
    public R<Void> saveAward(@Validated @RequestBody AwardBo bo) {
        awardService.saveAward(bo);
        return R.ok();
    }

    @Parameters({
            @Parameter(name = "ids", description = "荣誉主键ID(批量删除时可以传数组)", in = ParameterIn.PATH, required = true)
    })
    @Operation(summary = "删除律所荣誉信息", description = "删除律所荣誉信息")
    @SaCheckLogin
    @DeleteMapping("/award/{ids}")
    public R<Void> removeAward(@NotEmpty(message = "主键不能为空") @PathVariable("ids") Long[] ids) {
        awardService.deleteWithValidByIds(List.of(ids), true);
        return R.ok();
    }

    @Parameters({
            @Parameter(name = "id", description = "主键ID", in = ParameterIn.PATH, required = true)
    })
    @Operation(summary = "获取荣誉详情", description = "获取荣誉详情")
    @SaIgnore
    @GetMapping("/award/{id}")
    public R<AwardVo> getAward(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return R.ok(awardService.selectAwardVoById(id));
    }

    @Parameters({
            @Parameter(name = "lawFirmId", description = "律所ID", required = true),
            @Parameter(name = "avatar", description = "头像", required = true),
            @Parameter(name = "lawyerName", description = "姓名", required = true),
            @Parameter(name = "practice", description = "执业年限", required = true),
            @Parameter(name = "consultation", description = "咨询人数", required = true),
            @Parameter(name = "special", description = "擅长", required = true),
    })
    @Operation(summary = "律所添加律师信息", description = "律所添加律师信息")
    @SaCheckLogin
    @PostMapping("/saveLawyer")
    public R<Void> saveLawyer(@Validated @RequestBody LawyerInfoBo bo) {
        lawyerInfoService.saveLawyer(bo);
        return R.ok();
    }


    @Parameters({
            @Parameter(name = "ids", description = "律师主键ID(批量删除时可以传数组)", in = ParameterIn.PATH, required = true)
    })
    @Operation(summary = "删除律所律师信息", description = "删除律所律师信息")
    @SaCheckLogin
    @DeleteMapping("/lawyer/{ids}")
    public R<Void> removeLawyer(@NotEmpty(message = "主键不能为空") @PathVariable("ids") Long[] ids) {
        lawyerInfoService.deleteWithValidByIds(List.of(ids), true);
        return R.ok();
    }

    @Parameters({
            @Parameter(name = "id", description = "主键ID", in = ParameterIn.PATH, required = true)
    })
    @Operation(summary = "获取律师详情", description = "获取律师详情")
    @SaIgnore
    @GetMapping("/lawyer/{id}")
    public R<LawyerInfoVo> getLawyer(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        return R.ok(lawyerInfoService.selectLawyerVoById(id));
    }


    @Parameters({
            @Parameter(name = "lawFirmId", description = "律师律所ID", required = true)
    })
    @Operation(summary = "查看律师律所评价列表接口", description = "查看律师律所评价列表接口")
    @SaIgnore
    @GetMapping("/listComment")
    public TableDataInfo<SpuCommentVo> listLawFirmComment(SpuCommentBo bo, PageQuery pageQuery) {
        if (bo.getLawFirmId() == null) {
            bo.setLawFirmId(-1L);
        }
        bo.setStatus("1");
        return spuCommentService.selectPageList(bo, pageQuery);
    }

    @Parameters({
            @Parameter(name = "id", description = "评论主键ID", required = true)
    })
    @Operation(summary = "查看评论详情", description = "查看评论详情")
    @SaIgnore
    @GetMapping("/comment/{id}")
    public R<SpuCommentVo> getLawFirmComment(@NotNull(message = "主键不能为空")@PathVariable("id")Long id){
        return R.ok(spuCommentService.selectSpuCommentVoById(id));
    }


    @Operation(summary = "查询擅长列表接口", description = "律师擅长列表")
    @SaIgnore
    @GetMapping("/listExpertise")
    public R<List<SysDictDataVo>> listExpertise(){
        SysDictDataBo bo = new SysDictDataBo();
        bo.setDictType(SystemConstants.LAWYER_EXPERTISE);
        return R.ok(sysDictDataService.selectDictDataList(bo));
    }
}
